<?php
/**
 * 通用通知接口demo
 * ====================================================
 * 支付完成后，微信会把相关支付和用户信息发送到商户设定的通知URL，
 * 商户接收回调信息后，根据需要设定相应的处理流程。
 * 
 * 这里举例使用log文件形式记录回调信息。
*/
	include_once("WxPayPubHelper.php");
	error_log("====================notify_url.php============",0);
    echo "可访问";
    //使用通用通知接口
	$notify = new Notify_pub();

	//存储微信的回调
	$xml = $GLOBALS['HTTP_RAW_POST_DATA'];	
	$data = $notify->saveData($xml);
	//验证签名，并回应微信。
	//对后台通知交互时，如果微信收到商户的应答不是成功或超时，微信认为通知失败，
	//微信会通过一定的策略（如30分钟共8次）定期重新发起通知，
	//尽可能提高通知的成功率，但微信不保证通知最终能成功。
	if($notify->checkSign() == FALSE)
	{
		error_log("====================notify->checkSign() == FALSE============",0);
		$notify->setReturnParameter("return_code","FAIL");//返回状态码
		$notify->setReturnParameter("return_msg","签名失败");//返回信息
	}
	else
	{
		error_log("====================notify->checkSign() == true============",0);
	    if ($notify->data["return_code"] == "FAIL") 
	    {
			//此处应该更新一下订单状态，商户自行增删操作
		}
		elseif($notify->data["result_code"] == "FAIL")
		{
			//此处应该更新一下订单状态，商户自行增删操作

		}
		else
		{
		    $outTradeNo=$data['out_trade_no'];
			$transaction_id=$data['transaction_id'];
			$total_fee=floatval($data['cash_fee']/100);
			$trade_state=$data['result_code'];
			$create_time=$data['time_end'];
		//	window.location.href="http://tech.wawame.com/index.php?g=Wap&m=Userinfo&a=consultations&token=qkjedf1477883218";
			
			$host = "http://".$_SERVER['HTTP_HOST']."/index.php?g=Wap&m=Expert&a=addPayRecord&outtradeno=".$outTradeNo."&transactionid=".$transaction_id."&totalfee=".$total_fee."&tradestate=".$trade_state."&create_time=".$create_time;
			//$host="http://".$_SERVER['HTTP_HOST']."/Wap/Expert/addPayRecord/outtradeno/"
			// .$outTradeNo."/transactionid/".$transaction_id."/totalfee/".$total_fee."/tradestate/".$trade_state."/create_time/".$create_time;
			 error_log("add by hutingting          api/wxpay/notify_url  host=".$host,0);
	        $ch = curl_init($host); 	
	        $arr = array('Content-type: text/plain') ;
	       // curl_setopt ( $ch , CURL_HTTPHEADER , $arr );
            curl_setopt($ch, CURLOPT_URL, $host);  
            curl_setopt($ch, CURLOPT_HTTPHEADER,$arr);  
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); //设置自动跳转 
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); //尝试链接时间5s
            curl_setopt($ch, CURLOPT_TIMEOUT, 30); //执行获取时间5s
            curl_exec($ch);
            curl_close($ch);          
         	$notify->setReturnParameter("return_code","SUCCESS");//设置返回码				  
		}		
	}
	$returnXml = $notify->returnXml();
	error_log("weixin_notify_url".$returnXml."data".$data['out_trade_no'],0);
	echo $returnXml;
	
	//==商户根据实际情况设置相应的处理流程，此处仅作举例=======
	

/*	if($notify->checkSign() == TRUE)
	{
		if ($notify->data["return_code"] == "FAIL") {
			//此处应该更新一下订单状态，商户自行增删操作
		}
		elseif($notify->data["result_code"] == "FAIL"){
			//此处应该更新一下订单状态，商户自行增删操作

		}
		else{
		    $outTradeNo=$data['out_trade_no'];
			$transaction_id=$data['transaction_id'];
			$total_fee=floatval($data['cash_fee']/100);
			$trade_state=$data['result_code'];
			
            header('Location: http://store.dadulun.com/index.php?g=Wap&m=Product&a=addPayRecord&outTradeNo='
			   .$outTradeNo."&transactionid=".$transaction_id."&totalfee=".$total_fee."&tradestate=".$trade_state.''); 
         					  
		}
				
	} */
?>